package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public abstract class BaseStudentController {
    static StudentService studentService = new StudentService();
    static Scanner scanner = new Scanner(System.in);

    public void start() {
        stu:while (true){
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.print("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出     ");
            String choice = scanner.next();
            switch (choice){
                case "1":
//                    System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
//                    System.out.println("删除学生");
                    deleteById();
                    break;
                case "3":
//                    System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
//                    System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("退出完成");
                    break stu;
                default:
                    System.out.print("输入有误，请重新输入");
                    break;
            }
        }
    }

    public final void updateStudent() {
        //判断容器内是否存在元素
        boolean studentExist = new StudentService().studentExist();
        if (studentExist){
            System.out.print("请输入需要修改学号");
            while(true){
                String id = scanner.next();
                boolean exist = studentService.isExist(id);
                if (exist){
                    Student student = getStudent(id);

                    studentService.updateStudent(student);
                    System.out.println("修改成功");
                    break;
                }else{
                    System.out.println("输入学号不存在");
                    System.out.print("请重新输入");
                }
            }
        }else {
            System.out.println("暂无元素");
            System.out.println("欢迎添加");
        }

    }

    public final void deleteById() {
        boolean studentExist = new StudentService().studentExist();
        if (studentExist){
            System.out.print("请输入需删除学号");
            while(true){
                String id = scanner.next();
                boolean exist = studentService.isExist(id);
                if (exist){
                    studentService.deleteById(id);
                    System.out.println("删除成功");
                    break;
                }else{
                    System.out.println("学号不存在");
                    System.out.println("请重新输入");
                }
            }
        }else {
            System.out.println("暂无元素");
            System.out.println("欢迎添加");
        }

    }

    public final void findAllStudent() {
        Student[] stuArray = studentService.findAllStudent();
        if (stuArray == null){
            System.out.println("查无信息");
        }else{
//            System.out.println("学号\t\t姓名\t年龄\t生日");
            for (int i = 0; i < stuArray.length; i++) {
                if (stuArray[i] != null){
                    System.out.println(stuArray[i].toString() + "  ");
                }
            }
        }
    }

    public final void addStudent() {
        Scanner scanner = new Scanner(System.in);
        String id;
        while(true){
            System.out.print("请输入学号:");
            id = scanner.next();
            boolean flag = studentService.isExist(id);
            //如果存在true，继续输入
            if (flag){
                System.out.print("输入学号已存在，请重新输入");
            }else {
                break;
            }
        }
        Student student = getStudent(id);

        boolean result = studentService.addStudent(student);
        if (result == true){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }

    public abstract Student getStudent(String id);
}
